﻿#include "WRPopupEditBox.h"

//鼠标按下事件处理。
void WRPopupLineEdit::mousePressEvent(QMouseEvent *e)
{
    emit mousePressed(e);
}
//上下文菜单事件处理。
void WRPopupLineEdit::contextMenuEvent(QContextMenuEvent *)
{
    //add code here
}

//初始化弹出编辑框。
WRPopupEditBox::WRPopupEditBox(WRPopupControl* popup, QWidget* parent)
    : QComboBox(parent)
{
    m_popup = popup;
    m_popup->setParent(this, Qt::Popup | Qt::FramelessWindowHint);
    QObject::connect(m_popup, SIGNAL(closed()), this, SLOT(onPopupClosed()));
    m_text = new WRPopupLineEdit();
    m_text->setReadOnly(true);
    QObject::connect(m_text, SIGNAL(mousePressed(QMouseEvent*)), this, SLOT(onTextMousePressed(QMouseEvent*)));
    setLineEdit(m_text);
}

//清理当前对象所占用的相关资源。
WRPopupEditBox::~WRPopupEditBox()
{
    //add code here
}

//当文本鼠标按下处理。
void WRPopupEditBox::onTextMousePressed(QMouseEvent *)
{
    if (m_popup->isVisible())
    {
        m_popup->hide();
    }
    else
    {
        onPopup(m_popup);
        emit popup(m_popup);
        m_popup->show();
    }
}
//当弹出关闭时调用此方法。
void WRPopupEditBox::onPopupClosed(void)
{
    onClosed(m_popup);
    emit closed(m_popup);
}

//当弹出时调用此方法。
void WRPopupEditBox::onPopup(WRPopupControl*)
{
    QPoint pos = mapToGlobal(QPoint(0, 0));
    m_popup->move(pos);
}
//当关闭时调用此方法。
void WRPopupEditBox::onClosed(WRPopupControl*)
{
    //add code here
}

//当鼠标按下时调用此方法。
void WRPopupEditBox::mousePressEvent(QMouseEvent *)
{
    if (m_popup->isVisible())
    {
        m_popup->hide();
    }
    else
    {
        onPopup(m_popup);
        emit popup(m_popup);
        m_popup->show();
    }
}
